// const upyun = require('upyun')
// import * as qiniu from 'qiniu'
import qiniu = require('qiniu');
import { setInterval } from 'timers';

// const upyunClientService = new upyun.Service('file-mi','mathew','4230338l',{})
// const upyunClient = new upyun.Client(upyunClientService);

class OSSUploadManger {
  // static upyunClient = new upyun.Client(upyunClientService);
  private uploadToken = null;
  private timer = null;

  constructor() {
    this.initSdk();
  }

  private initSdk() {
    const accessKey = 'm1syJtZt97vSWT6sPrIce_dTRACwFtBViffPbIVA';
    const secretKey = '0fdH7wRdY2F7gdTJeqA74JqT0uA2xM1SvOI8_aCE';
    const mac = new qiniu.auth.digest.Mac(accessKey, secretKey);

    // 上传凭证
    const putPolicy = new qiniu.rs.PutPolicy({
      scope: 'file-mi',
      expires: 7200,
    });
    this.uploadToken = putPolicy.uploadToken(mac);
  }

  // tslint:disable-next-line:no-any
  uploadFile(keyPath: string, body: any, option?: qiniu.form_up.PutExtra) {
    const config = new qiniu.conf.Config({ zone: qiniu.zone.Zone_z0 });
    const formUploader = new qiniu.form_up.FormUploader(config);
    const putExtra = option || new qiniu.form_up.PutExtra();
    // let key = keyPath;

    return new Promise((resolve, reject) => {
      formUploader.put(
        this.uploadToken,
        keyPath,
        body,
        putExtra,
        (err, res, info) => {
          if (err) {
            reject(err);
          } else {
            resolve(res);
          }
        },
      );
    });
  }
}
const UploadManger = new OSSUploadManger();
export default UploadManger;
